package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingOrderService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.vo.invoice;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.sun.jdi.IntegerType;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.*;
import java.util.List;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

@Controller
@RequestMapping("cargo/invoice")
public class InvoiceController extends BaseController {

    @Reference
    private InvoiceService invoiceService;
    @Reference
    private ShippingOrderService shippingOrderService;

    @Reference
    private ContractService contractService;

    @Reference
    private PackingService packingService;

    @RequestMapping(value = "/list", name = "显示财务列表")

    public String findAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize) {
        InvoiceExample example = new InvoiceExample();
        example.createCriteria().andCompanyIdEqualTo(getCompanyId());
        PageInfo<Invoice> pageInfo = invoiceService.findAll(example, page, pageSize);
        request.setAttribute("page", pageInfo);
        return "cargo/invoice/invoice-list";
    }


    @RequestMapping(value = "/delete", name = "删除购销合同货物方法")
    public String delete(String id) {
       /* InvoiceExample example = new InvoiceExample();
        example.createCriteria().andInvoiceIdEqualTo(id);*/
        invoiceService.deleteById(id);

        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/toAdd", name = "新增")
    public String toadd(String id) {
        //查询报运合同号、贸易条款
        //自己计算发票金额
        //提单凭据
        request.setAttribute("id", id);
        Packing packing = packingService.findById(id);
        String exportNos = packing.getExportNos();
        request.setAttribute("exprotNos", exportNos);

        request.setAttribute("tradeTerms", "贸易条款假数据");


        //String packingMoney = packing.getPackingMoney().toString();//inovice需要整数
        int packingMoney = packing.getPackingMoney().intValue();
        request.setAttribute("packingMoney", packingMoney);

        return "/cargo/invoice/invoice-add";
    }


    @RequestMapping(value = "edit", name = "保存发票")
    public String edit(Invoice invoice) {

        invoice.setCompanyId(getCompanyId());
        invoice.setState(1);
        invoiceService.insertinvoice(invoice);
        return "redirect:/cargo/invoice/list.do";

    }



    @Autowired
    private DownloadUtil downloadUtil;

   /* @RequestMapping(value = "/invoiceE", name = "导出excel表格")
    public void invoiceE(String id) throws IOException {
        List<invoice> invoices = invoiceService.findinvoiceById(id);
        for (invoice invoice : invoices) {
            System.out.println(invoice.getInvoice_no());
        }
        String realPath = session.getServletContext().getRealPath("/template/7-发票.xlsx");
        //获取指定的工作簿
        Workbook workbook = new XSSFWorkbook(new FileInputStream(realPath));

        //获取工作表
//        Sheet sheet1 = workbook.getSheet("");//通过名字获取工作簿
        Sheet sheet = workbook.getSheetAt(0);//通过索引获取工作簿

        Row row = sheet.getRow(3);//通过工作簿    获取指定索引的行
        Cell cell = row.getCell(0);//通过指定的行 获取指定索引的列
        cell.setCellValue(invoices.get(0).getSeller());//放入值invoices.get(0).getSeller()

        Row row1 = sheet.getRow(6);
        Cell cell1 = row1.getCell(0);
        cell1.setCellValue(invoices.get(0).getBuyer());//

        Row row2 = sheet.getRow(11);
        Cell cell2 = row2.getCell(0);
        cell2.setCellValue(invoices.get(0).getInvoice_no());//
//
//
        Row row3 = sheet.getRow(11);
        Cell cell3 = row3.getCell(2);
        cell3.setCellValue(invoices.get(0).getInvoice_date());//

        Row row4 = sheet.getRow(13);
        Cell cell4 = row4.getCell(5);
        cell4.setCellValue(invoices.get(0).getShipment_port());//
//
        Row row5 = sheet.getRow(13);
        Cell cell5 = row5.getCell(7);
        cell5.setCellValue(invoices.get(0).getDestination_port());//
//
//
        Row row6 = sheet.getRow(15);
        Cell cell6 = row6.getCell(2);
        cell6.setCellValue(invoices.get(0).getDescriptions());//

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        downloadUtil.download(byteArrayOutputStream, response, "发票单.xlsx");
    }*/
}
